package com.example.plant02

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView


class PlantAdapter(val plants:ArrayList<PlanT>):RecyclerView.Adapter<PlantAdapter.PlantViewHodler>() {

    inner class PlantViewHodler(item:View): RecyclerView.ViewHolder(item){
        val name=item.findViewById<TextView>(R.id.name)
        val introduction=item.findViewById<TextView>(R.id.introduction)
        val img = item.findViewById<ImageView>(R.id.pic)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlantViewHodler {
        val view =LayoutInflater.from(parent.context).inflate(R.layout.item_introduct,parent,false)
        val holder=PlantViewHodler(view)
        return  holder
    }

    override fun onBindViewHolder(holder: PlantViewHodler, position: Int) {
        val plant = plants[position]
        holder.name.text=plant.name
        holder.introduction.text=plant.introduction
        plant.pic?.let { holder.img.setImageResource(it) }
    }

    override fun getItemCount(): Int {
        return plants.size
    }
}